<?php

function image_resize($src, $dst, $width, $height, $crop=0, $ysize=false){

	if(!list($w, $h) = getimagesize($src)) print "Unsupported picture type 1!";

	$type = strtolower(substr(strrchr($src,"."),1));
	if($type == 'jpeg') $type = 'jpg';
	switch($type){
		case 'bmp': $img = imagecreatefromwbmp($src); break;
		case 'gif': $img = imagecreatefromgif($src); break;
		case 'jpg': $img = imagecreatefromjpeg($src); break;
		case 'png': $img = imagecreatefrompng($src); break;
		default : print "Unsupported picture type 2!";
	}

	// resize
	if($crop){
		//if($w < $width or $h < $height) print "Picture is too small!";
		$ratio = max($width/$w, $height/$h);
		$h = $height / $ratio;
		$x = ($w - $width / $ratio) / 2;
		if ($ysize == true)
		{
			$y = ($w - $height / $ratio) / 2;
		}
		$w = $width / $ratio;
	}
	else{
		//if($w < $width and $h < $height) print "Picture is too small!";
		$ratio = min($width/$w, $height/$h);
		$width = $w * $ratio;
		$height = $h * $ratio;
		$x = 0;
		$y = 0;
	}

	$new = imagecreatetruecolor($width, $height);

	// preserve transparency
	if($type == "gif" or $type == "png"){
		imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
		imagealphablending($new, false);
		imagesavealpha($new, true);
	}

	imagecopyresampled($new, $img, 0, 0, $x, $y, $width, $height, $w, $h);

	switch($type){
		case 'bmp': imagewbmp($new, $dst); break;
		case 'gif': imagegif($new, $dst); break;
		case 'jpg': imagejpeg($new, $dst); break;
		case 'png': imagepng($new, $dst); break;
	}
	return true;
}

if ($_POST['action']=="save"){
	
	/*
	print "<pre>";
	print_r($_FILES);
	print "</pre>";
	*/
	
	// végigszaladunk az indexeken
	for ($i = 1; $i <= 4; $i++) {
		
		// feltöltjük a képet ha van
		if ($_FILES['thumb_'.$i]['error']==0 && $_FILES['thumb_'.$i]['type']=="image/png"){
			
			//print $i."<br>";
			move_uploaded_file($_FILES['thumb_'.$i]['tmp_name'],"../images/image.png");
			image_resize("../images/image.png", "../images/".$i.".png", "640", "480",true);
			image_resize("../images/image.png", "../images/thumb_".$i.".png", "320", "240",true);
		}
		
	}
	
	unlink("../images/image.png");
}

$page_title = "Keretek szerkesztése";
$page_text = "

<form action='index.php?border' method='post' enctype='multipart/form-data'>
<input type='hidden' name='action' value='save' />

	<div class='fieldset'>
		<div class='legend'>
			<span class='left title'>1. keret</span>
			<span class='ui-icon ui-icon-info help-box left' info-title='' info-text=''></span>
		</div>
		<div class='clear'></div>
		<img src='../images/thumb_1.png' /><br /><br />
		<input type='file' name='thumb_1' class='file ui-widget-content ui-state-default ui-corner-all'>
	</div>
	
	<div class='fieldset'>
		<div class='legend'>
			<span class='left title'>2. keret</span>
			<span class='ui-icon ui-icon-info help-box left' info-title='' info-text=''></span>
		</div>
		<div class='clear'></div>
		<img src='../images/thumb_2.png' /><br /><br />
		<input type='file' name='thumb_2' class='file ui-widget-content ui-state-default ui-corner-all'>
	</div>
	
	<div class='fieldset'>
		<div class='legend'>
			<span class='left title'>3. keret</span>
			<span class='ui-icon ui-icon-info help-box left' info-title='' info-text=''></span>
		</div>
		<div class='clear'></div>
		<img src='../images/thumb_3.png' /><br /><br />
		<input type='file' name='thumb_3' class='file ui-widget-content ui-state-default ui-corner-all'>
	</div>
	
	<div class='fieldset'>
		<div class='legend'>
			<span class='left title'>4. keret</span>
			<span class='ui-icon ui-icon-info help-box left' info-title='' info-text=''></span>
		</div>
		<div class='clear'></div>
		<img src='../images/thumb_4.png' /><br /><br />
		<input type='file' name='thumb_4' class='file ui-widget-content ui-state-default ui-corner-all'>
	</div>
	
	<div class='admin_button'>
		<input type='submit' class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' value='Mentés'>
	</div>

</form>

";

?>